﻿#pragma once
#include<iostream>
#include<assert.h>
#include<string.h>
#include<algorithm>
using namespace std;

namespace Cw
{
	class string
	{
		public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		} 
		iterator end()
		{
			return _str + _size;
		} 
		const_iterator begin() const
		{
			return _str;
		} 
		const_iterator end() const
		{
			return _str + _size;
		} 

		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			cout << "string(char* str)-构造" << endl;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		} 

		void swap(string& s)
		{
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		} 

		//拷贝构造
		string(const string& s)
			:_str(nullptr)
		{
			cout <<"string(const string& s) -- 拷贝构造" << endl;
			reserve(s._capacity);
			for (auto ch : s)
			{
				push_back(ch);
			}
		} 
		//// 移动构造
		//string(string && s)
		//{
		//	cout <<"string(string&& s) -- 移动构造" << endl;
		//	swap(s);
		//} 

		string& operator=(const string& s)
		{
			cout <<"string& operator=(const string& s) -- 拷贝赋值" <<endl;
			if (this != &s)
			{
				_str[0] = '\0';
				_size = 0;
				reserve(s._capacity);
				for (auto ch : s)
				{
					push_back(ch);
				}
			} 
			return* this;
		} 

		// 移动赋值
		/*string & operator=(string && s)
		{
			cout << "string& operator=(string&& s) -- 移动赋值" << endl;
			swap(s);
			return *this;
		}*/

		~string()
		{
			cout << "~string() -- 析构" << endl;
			delete[] _str;
			_str = nullptr;
		}
		
		char & operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		} 

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				if (_str)
				{
					strcpy(tmp, _str);
					delete[] _str;
				}
				_str = tmp;
				_capacity = n;
			}
		} 
		void push_back(char ch)
		{
			if (_size >= _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity *2;
				reserve(newcapacity);
			} 
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		const char* c_str() const
		{
			return _str;
		}

	   size_t size() const
		{
			return _size;
		}

	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;
	};
}

